// import type { number } from 'echarts/core'

/**
 * 用于划小地区、。。。。
 * @param tree 树形结构
 * @returns
 */
export const convertTree = (tree: any) => {
  return tree.map((node: any) => {
    const convertedNode = {
      value: node.id,
      label: node.text,
      children: node.children ? convertTree(node.children) : undefined
    }
    return convertedNode
  })
}

export const convertTreeLabel = (tree: any) => {
  return tree.map((node: any) => {
    const convertedNode = {
      value: node.id,
      label: node.label,
      children: node.children ? convertTreeLabel(node.children) : undefined
    }
    return convertedNode
  })
}

export const convertTreeChecked = (tree: any) => {
  return tree.map((node: any) => {
    const convertedNode = {
      children: node.children ? convertTreeChecked(node.children) : undefined,
      id: node.id,
      parent: node.parent,
      checked: node.checked,
      label: node.text
    }
    return convertedNode
  })
}

export const YingYongTree = (tree: any) => {
  return tree.map((node: any) => {
    const convertedNode = {
      attributes: node.attributes ? node.attributes : undefined,
      // checked: node.checked,
      children: node.children ? YingYongTree(node.children) : undefined,
      id: node.id,
      parent: node.parent,
      state: node.state,
      label: node.text
    }
    return convertedNode
  })
}

export const findLabelByValue: any = (arr: any, value: number) => {
  console.log('value', value)

  // 遍历数组中的每个节点
  for (let i = 0; i < arr.length; i++) {
    const node = arr[i]
    // 如果当前节点的 value 与目标 value 匹配，返回对应的 label
    if (node.value === value) {
      return node.label
    }
    // 如果当前节点有 children 属性，递归地在子节点中查找
    if (node.children) {
      const result = findLabelByValue(node.children, value)
      // 如果在子节点中找到了对应的 label，返回它
      if (result) {
        return result
      }
    }
  }
  // 如果遍历完所有节点都没有找到匹配的 value，返回 null 或其他适当的值
  return null
}

/**
 * 时间转换 YYYY-MM-DD HH:mm:ss / YYYY-MM
 */
export const timeFormat = (time: any) => {
  const date = new Date(time)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const monthStr = String(month).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  const hours = String(date.getHours()).padStart(2, '0')
  const minutes = String(date.getMinutes()).padStart(2, '0')
  const seconds = String(date.getSeconds()).padStart(2, '0')
  // 构造当前月份的第一天
  const firstDayOfMonth = new Date(year, month - 2, 1)

  return {
    MM: month,
    YYYYMM: `${year}-${monthStr}`,
    YYYYMMDD: `${year}-${monthStr}-${day}`,
    firstDayOfMonth: firstDayOfMonth,
    YYYYMMDDHHMMSS: `${year}-${monthStr}-${day} ${hours}:${minutes}:${seconds}`
  }
}

/**
 *
 * @param obj 对象
 * @param key key
 * @returns 对象
 */
export const filterByKey = (obj: any, key: any) => {
  return Object.keys(obj)
    .filter((k) => k.startsWith(key))
    .reduce(
      (acc: any, k) => {
        acc[k] = obj[k]
        return acc
      },
      {
        info: key
      }
    )
}

export const filterByPrefix = (obj: any, prefix: any) => {
  return Object.keys(obj)
    .filter((k) => !k.includes(prefix))
    .reduce((acc: any, k) => {
      acc[k] = obj[k]
      return acc
    }, {})
}
